package com.yonyou.iuap.controller.app;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.pub.IDFactory;
import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.entity.KmFavoriteEntity;
import com.yonyou.iuap.shell.entity.exp.FavoritePojo;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IKmFavoriteService;
/**
 * 知识收藏表
 * **/
@Controller
@RequestMapping("/favorite")
public class KmfavoriteEntityController {
	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
	@Autowired
	private IKmFavoriteService iKmFavoriteService;
	
	/**
	 * 根據用戶查詢收藏
	 * 
	 * ***/
	@RequestMapping(value="App/selectfavoriteuser",method=RequestMethod.POST)
	@ResponseBody
	public String selectfavoriteuser(HttpServletRequest resq,@RequestBody Map parMap){
		start=System.currentTimeMillis();
		ResultDto resultDto=new ResultDto();
		if(parMap.get("publish_key")!=null && !StringUtils.isEmpty(parMap.get("publish_key").toString())){
			//根据关键字查询收藏列表
			String publish_key=parMap.get("publish_key").toString();
			StringBuffer str=new StringBuffer("");
			if(!StringUtils.isEmpty(publish_key)){
				if(publish_key.indexOf(",")>=0){
					str.append("(");
					for(String key : publish_key.split(",")){
						str.append(" p.publish_key like '%"+key+"%' ").append(" or");
					}
					str.delete(str.toString().length()-2, str.toString().length());
					str.append(")");
				}else{
					str.append(" p.publish_key like '%"+publish_key+"%' ");
				}
			}
			parMap.put("publish_key", str.toString());
			resultDto = iKmFavoriteService.Selectfavorite(parMap);
		}else if(parMap.get("favorite_user")!=null && !StringUtils.isEmpty(parMap.get("favorite_user").toString())){
			//根据用户查询所有收藏列表
			String favorite_user=parMap.get("favorite_user").toString();
			StringBuffer str=new StringBuffer();
			str.append(" f.favorite_user='"+favorite_user+"'");
			resultDto = iKmFavoriteService.queryfavoriteuser(parMap);
			ResultDto resultDto1=	iKmFavoriteService.favoritecount(str.toString());
			resultDto.setData1(resultDto1.getData());
		}else{
			resultDto.setStatus(1);
			resultDto.setMessage("参数错误");
		}
		
		String rtn_json = JsonUtils.toJsonStr(resultDto);
		end=System.currentTimeMillis();
		logger.info("selectfavoriteuser接口调用时间"+String.valueOf(end-start));
		return rtn_json;
	}
	
	
	/**
	 * 根據關鍵字查詢收藏
	 * 
	 * **/
	@RequestMapping(value="App/Queryfavorite",method=RequestMethod.POST)
	@ResponseBody
	public String Queryfavorite(HttpServletRequest resq,@RequestBody Map parMap){
		start=System.currentTimeMillis();
		String publish_key=parMap.get("publish_key").toString();
		StringBuffer str=new StringBuffer("");
		if(!StringUtils.isEmpty(publish_key)){
			if(publish_key.indexOf(",")>=0){
				str.append("(");
				for(String key : publish_key.split(",")){
					str.append(" p.publish_key like '%"+key+"%' ").append(" or");
				}
				str.delete(str.toString().length()-2, str.toString().length());
				str.append(")");
			}else{
				str.append(" p.publish_key like '%"+publish_key+"%' ");
			}
		}
		parMap.put("publish_key", str.toString());
		ResultDto resultDto = iKmFavoriteService.Selectfavorite(parMap);
		String rtn_json = JsonUtils.toJsonStr(resultDto);
		end=System.currentTimeMillis();
		logger.info("Queryfavorite接口调用时间"+String.valueOf(end-start));
		return rtn_json;
	}

	
	
	@RequestMapping(value="App/insertfavorite",method=RequestMethod.POST)
	@ResponseBody
	public String insertfavorite(HttpServletRequest resq,@RequestBody Map parMap){
		start=System.currentTimeMillis();
		String pk_user=parMap.get("pk_user").toString();
		String pk_publish=parMap.get("pk_publish").toString();
		KmFavoriteEntity supplier=new KmFavoriteEntity();
		supplier.setFavorite_user(pk_user);
		supplier.setPk_publish(pk_publish);
		supplier.setPk_favorite(IDFactory.getInstance().getID());
		SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		supplier.setFavorite_time(dd.format(new Date()));
		supplier.setSealmark("N");
		supplier.setDtcreate(new Date());
		supplier.setDr(0);
		supplier.setFW_TS(new Date());
		ResultDto resultDto = iKmFavoriteService.insert(supplier);
		end=System.currentTimeMillis();
		logger.info("insertfavorite接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(resultDto);
	}
	@RequestMapping(value="Countfavorite",method=RequestMethod.POST)
	@ResponseBody
	public String Countfavorite(HttpServletRequest resq,@RequestBody Map parMap){
		start=System.currentTimeMillis();
		List<FavoritePojo> Pojo=iKmFavoriteService.Countfavoritepk();
		String rtn_json = JsonUtils.toJsonStr(Pojo);
		end=System.currentTimeMillis();
		logger.info("Countfavorite接口调用时间"+String.valueOf(end-start));
		return rtn_json;
	}
	
	/*
	 * 取消收藏
	 * 
	 * **/
	
	@RequestMapping(value="App/deletefavorite",method=RequestMethod.POST)
	@ResponseBody
	public String deletefavorite(HttpServletRequest resq,@RequestBody Map parMap){
		start=System.currentTimeMillis();
		String pk_favorite=parMap.get("pk_favorite").toString();
		String pk_user=parMap.get("pk_user").toString();
		StringBuffer str=new StringBuffer();
		str.append("pk_publish='"+pk_favorite+"' and favorite_user='"+pk_user+"'");
		ResultDto resultDto = iKmFavoriteService.deleteByCondition(str.toString());
		String rtn_json = JsonUtils.toJsonStr(resultDto);
		end=System.currentTimeMillis();
		logger.info("deletefavorite接口调用时间"+String.valueOf(end-start));
		return rtn_json;
	}
	
	
	/**
	 * 
	 * 查詢收藏关键字
	 * **/
	
	@RequestMapping(value="App/favoritecount",method=RequestMethod.POST)
	@ResponseBody
	public String favoritecount(HttpServletRequest resq,@RequestBody Map parMap){
		start=System.currentTimeMillis();
		String favorite_user=parMap.get("favorite_user").toString();
		StringBuffer str=new StringBuffer();
		str.append(" f.favorite_user= '"+favorite_user+"'");
		ResultDto resultDto=	iKmFavoriteService.favoritecount(str.toString());
		String rtn_json = JsonUtils.toJsonStr(resultDto);
		end=System.currentTimeMillis();
		logger.info("favoritecount接口调用时间"+String.valueOf(end-start));
		return rtn_json;
	}
	
	
	
	
	
}
